﻿using System;
using System.ComponentModel.Composition;
using System.Data;
using System.Linq;
using System.Data.Entity;
using System.Linq.Expressions;
using ASPItalia.ModelVirtualCasting.Common;
using ASPItalia.ModelVirtualCasting.ObjectModel;
using ASPItalia.ModelVirtualCasting.Parts;

namespace ASPItalia.ModelVirtualCasting.EntityFramework
{
	[Export("UserRepository", typeof(IUserRepository))]
	[PartCreationPolicy(CreationPolicy.NonShared)]
	[RepositoryMetadata(Name = "UserRepository", IsFake = false)]
	public class UserRepository : BaseRepository<User>, IUserRepository
	{
		[ImportingConstructor()]
		public UserRepository(IContextContainer container)
			: base(container)
		{
		}

		public void InsertOnSubmit(User entity)
		{
			CurrentSet.Add(entity);
		}

		public void Update(User entity)
		{
			CurrentSet.Attach(entity);
			Context.ChangeObjectState(entity, EntityState.Modified);
		}

		public void DeleteOnSubmit(User entity)
		{
			CurrentSet.Remove(entity);
		}

		public void SubmitChanges()
		{
			SaveChanges();
		}

		public IQueryable<User> Where(Expression<Func<User, bool>> predicate)
		{
			return CurrentQueryableSet.Where(predicate);
		}

		public long Count(Expression<Func<User, bool>> predicate)
		{
			return CurrentQueryableSet.Count(predicate);
		}

		public User GetByUsername(string username)
		{
			return CurrentQueryableSet.First(u => u.Username == username);
		}

		public IQueryable<User> GetAll()
		{
			return CurrentQueryableSet;
		}

		public IUserRepository Include<TProperty>(Expression<Func<User, TProperty>> path)
		{
			IncludeInternal(path);
			return this;
		}

		public IUserRepository Include(string path)
		{
			IncludeInternal(path);
			return this;
		}

		public IUserRepository CreateQuery()
		{
			CreateQueryInternal();
			return this;
		}

		public IUserRepository EnableLazyLoading()
		{
			SetLazyLoading(true);
			return this;
		}

		public IUserRepository DisableLazyLoading()
		{
			SetLazyLoading(false);
			return this;
		}

		public IUserRepository DoNotTrack()
		{
			base.DoNotTrackInternal();
			return this;
		}
	}
}